import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/presenter/review_presenter.dart';
import 'package:app/src/ui/app_ui.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/review_details_ui.dart';
import 'package:app/src/ui/route/worksdo/type26_my_workorder/public/items/ui_myworkorder.dart';
import 'package:app/src/ui/route/worksdo/type26_my_workorder/public/items/ui_myworkorder2.dart';
import 'package:app/src/ui/route/worksdo/type26_my_workorder/public/items/ui_other.dart';
import 'package:app/src/ui/route/worksdo/type26_my_workorder/public/items/ui_receipt.dart';

import '../../../../http/bean/http_data.dart';
import '../../../../http/bean/worksdo/equipment_management_bean.dart';
import '../../../../http/bean/worksdo/equipment_management_bean.dart';
import '../../../../http/bean/worksdo/material_requisition_bean.dart';
import '../../../../http/bean/ywx_limit.dart';
import '../../../../http/model/ajaxsales_model.dart';
import '../../../../http/model/equipment_management_model.dart';
import '../../../dialog/dailog.dart';
import '../../helper/route_helper.dart';
import '../../works/public/image/ui_image_upload.dart';
import '../../works/public/image/ui_image_upload2.dart';
import '../../works/public/items/ui_note.dart';
import '../../works/public/review_submitted.dart';
import '../type26_my_workorder/public/review_submitted_myworkorder.dart';
import 'public/items/ui_attachment.dart';
import 'public/acom/ui_ticketcenter_form.dart';
import 'public/items/ui_myworkorder3.dart';

///我的工单详情
class MyworkorderDetailsRoute extends StatefulWidget {
  @override
  _State createState() => _State();

  EquipmentInfo? data;
  YwxItem item;

  MyworkorderDetailsRoute(this.data, this.item);
}

class _State extends State<MyworkorderDetailsRoute> {
  _Presenter presenter = _Presenter();

  @override
  void initState() {
    super.initState();
    presenter?.data = widget.data;
    ///延迟获取数据
    presenter.GetRepairPersonListWithDelay(context, callback: () {});
  }

  @override
  Widget build(BuildContext context) {
    return ReviewDetailsUi.build(context, presenter, _body,
        title: AppStrings.of(context).Myworkorder); //我的工单
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    UiImageUpLoad.clear();
    UiImageUpLoad2.clear();
    AudioplayersUtils.dispose(); //fixme 关闭音频播放。
  }

  Widget _body(BuildContext context) {
    List<Widget> children = [];
    if (presenter.data?.isOtherRepairType() ?? false) {
      //其他派单
      children.add(UiOther.build(
        context,
        presenter?.myRepairOrderInfoBean,
        setState,
      )); //设备报修
      children.add(UiNote.build(context,
          presenter?.myRepairOrderInfoBean?.OrderInfo?.Remark, null)); //备注
    } else {
      //待派单
      children.add(UiMyWorkOrder.build(
        context,
        presenter?.myRepairOrderInfoBean,
        setState,
      )); //设备报修
      children.add(UiMyWorkOrder2.build(
        context,
        presenter?.myRepairOrderInfoBean,
        setState,
      )); //二级设备信息
      children.add(UiMyWorkOrder3.build(
        context,
        presenter?.myRepairOrderInfoBean,
        setState,
      )); //三级设备信息
    }
    children.add(UiAttachment.build(context, presenter?.data?.getFiles(),
        isDel: false)); //附件
    if (presenter?.data?.isState2(presenter?.maintenanceManInfos) ?? false) {
      //接单没有图片视频上传，接单之后的完工有。
      if (presenter?.data?.isRepairType1() ?? false) {
        //fixme 强制险工单，没有五金领用。
      } else {
        children.add(UiReceipt.build(context, presenter?.data, presenter?.data,
            setState, widget.item)); //工单领用
      }
      children.add(UiImageUpLoad2.build(
          context, presenter?.mDatas?.Material?.getId(),
          title: AppStrings.of(context).UploadEvidence3)); //上传视频图片
    }

    return Column(
      children: [
        Expanded(child: UiBase.listView(context, children)),
        Visibility(
            child: ReviewSubmittedStateful2(
              presenter?.getEquipmentResponseData(),
              callbackWithT: (subData) {
                //fixme 提交参数数据
              },
              callbackWithBoolResult: () {
                //fixme 提交数据格式验证
                if (presenter?.data?.isState2(presenter?.maintenanceManInfos) ?? false) {
                  //fixme 已接单 ->完工;
                  var length = UiImageUpLoad2.files?.length ?? 0;
                  if (length <= 0) {
                    AppDialog.showDialogWithInfo(context,
                        content: AppStrings.of(context)
                            .UploadEvidence4); //请至少上传一张图片或一个视频
                    return false;
                  }
                }
                return true;
              },
              callbackWithBool: (value) {
                //fixme 提交结果返回
                if (value) {
                  //接单成功，重新刷新数据。
                  presenter?.GetRepairPersonList(context, callback: () {});
                }
              },
            ),
            visible: presenter?.data != null)
      ],
    );
  }
}

class _Presenter extends ReviewPresenter {
  //工单信息
  EquipmentInfo? data = null;

  //维修人员
  List<MaintenanceManInfo>? maintenanceManInfos = null;

  //我的订单详情数据
  GetMyRepairOrderInfoBean? myRepairOrderInfoBean = null;

  //提交数据
  EquipmentResponseData? _equipmentResponseData = null;

  EquipmentResponseData getEquipmentResponseData() {
    if (_equipmentResponseData == null) {
      _equipmentResponseData = EquipmentResponseData();
    }
    _equipmentResponseData?.equipmentInfo = data;
    _equipmentResponseData?.maintenanceManInfos = maintenanceManInfos;
    return _equipmentResponseData!;
  }

  ///获取详情数据
  GetRepairPersonList(BuildContext context,
      {ResponseCallbackWithVoid? callback = null}) {
    isLoad = true;
    if (data == null) {
      return;
    }
    EquipmentManagementModel.GetMyRepairOrderInfo(context, data!, true,
        onError: (code, error) {
      isLoad = false;
      setStateBody?.call(() {});
      AppDialog.showDialogWithError(context, code, error)?.then((value) {
        AppRouteHelper.pop(context); //网络异常，关闭当前页面
      });
    })?.then((value) {
      isLoad = false;
      if (value.isSuccessForCode()) {
        try {
          setStateBody?.call(() {
            myRepairOrderInfoBean = value?.Data;
            _equipmentResponseData?.myRepairOrderInfoBean =
                myRepairOrderInfoBean;
            data = value?.Data?.OrderInfo;
            maintenanceManInfos = value?.Data?.RepairPersonInfo;
          });
          callback?.call();
        } catch (e) {}
      } else {
        //异常
        AppDialog.showDialogWithInfo(context, content: value.MsgDetail)
            ?.then((value) {
          AppRouteHelper.pop(context); //关闭当前页面
        });
      }
    });
  }

  ///获取审核详情数据[延迟获取，等待页面加载完成]
  GetRepairPersonListWithDelay(BuildContext context,
      {ResponseCallbackWithVoid? callback = null}) {
    Future.delayed(Duration(milliseconds: 200)).then((e) {
      GetRepairPersonList(context, callback: callback);
    });
  }
}
